package com.ruoyi.common.utils;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransCommonQueryModel;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransCommonQueryRequest;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayFundTransCommonQueryResponse;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.ruoyi.project.system.domain.SmExtract;
import com.ruoyi.project.system.domain.SmPay;
import lombok.extern.slf4j.Slf4j;
import java.math.RoundingMode;

@Slf4j
public class AlibabaApi {

    public static AlipayFundTransUniTransferResponse transfer(SmExtract smExtract) throws AlipayApiException {
//        if (1 == 1){
//            System.out.println("-------执行转账--------"+smExtract);
//            return null;
//        }
        // 初始化SDK
        AlipayClient alipayClient = getAlipayConfig();
        // 构造请求参数以调用接口
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();
        // 设置商户转账唯一订单号
        model.setOutBizNo(smExtract.getExtractId());
        // 设置转账金额
        model.setTransAmount(smExtract.getAmount().toString());
        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");
        // 设置业务产品码
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        // 设置转账业务的标题
        model.setOrderTitle("提现转账");
        // 设置收款方信息
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentityType("ALIPAY_LOGON_ID");
        payeeInfo.setIdentity(smExtract.getAlipayAccount());
        payeeInfo.setName(smExtract.getAlipayName());
        model.setPayeeInfo(payeeInfo);
        request.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        log.warn(response.getBody());
        return response;
    }

    public static AlipayFundTransCommonQueryResponse query(SmExtract smExtract) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = getAlipayConfig();
        // 构造请求参数以调用接口
        AlipayFundTransCommonQueryRequest request = new AlipayFundTransCommonQueryRequest();
        AlipayFundTransCommonQueryModel model = new AlipayFundTransCommonQueryModel();
        // 设置销售产品码
        model.setProductCode("TRANS_ACCOUNT_NO_PWD");
        // 设置描述特定的业务场景
        model.setBizScene("DIRECT_TRANSFER");
        // 设置商户转账唯一订单号
        model.setOutBizNo(smExtract.getExtractId());
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }

    public static AlipayTradeRefundResponse refund(SmPay smPay) throws AlipayApiException {
//        if (1 == 1){
//            System.out.println("-------执行退款--------"+smPay);
//            return null;
//        }
        // 初始化SDK
        AlipayClient alipayClient = getAlipayConfig();
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        // 设置商户订单号
        model.setOutTradeNo(smPay.getPlatformOrderNo());
        // 设置部分退款单号
        model.setOutRequestNo(smPay.getRemark());
        // 设置退款金额
        model.setRefundAmount(smPay.getPricing().setScale(2, RoundingMode.HALF_UP).toString());
        request.setBizModel(model);
        return alipayClient.certificateExecute(request);
    }

    private static DefaultAlipayClient getAlipayConfig() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        //设置应用APPID
        alipayConfig.setAppId("2021005141671074");
        //设置应用私钥
        alipayConfig.setPrivateKey("MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCL/Jw1JwIci1ImJtm6u5ABQ8NbPOpH9PLI4+IH/IJaZlPTEWbtx2/U0GLyQFgNtvAHd4cz+yTrVdbBcdLoRE6lcK6TF1ZQDMseyMdTxBWXeZ9d6aG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hiXOiIi9H7gVbLaAtywijivAoGBAIHv+Yb1jNaesgLcRaBHken/aqFAZ6RNMzUrJ7wtDpnciDO6Hvki0x9AxD+hgEK9u3HwB5DHpfB4tU0i5Mr+V+sLoBKWTDDJybg5fOIr+/G5gejnvaEPEh11aHYog491mM5a2Q6DnO/OQ06W+VBaxCE/1KRAB6YoQoNliM8eguuJAoGBAIkoLS4Sp5b1gGxcDFMV/MeCbCFeKYfOVsYGLz+vGTMX9ynL5v/0KUPWFuZxsHWkKB1RMtI6gVmFcdWtuqzYiw8owo4njJQ93L6l9G4qQczgeANX/tKg/SHe1i694AphoO0KwgZNgwfNEsPNXNFH4zrQDNT4a27+s6nkjonSZi1o");
        alipayConfig.setAppCertPath("/foo/appCertPublicKey_2021005141671074.crt");
        //设置支付宝公钥证书路径
        alipayConfig.setAlipayPublicCertPath("/foo/alipayCertPublicKey_RSA2.crt");
        //设置支付宝根证书路径
        alipayConfig.setRootCertPath("/foo/alipayRootCert.crt");
        //设置请求格式，固定值json
        alipayConfig.setFormat("json");
        //设置字符集
        alipayConfig.setCharset("UTF-8");
        //设置签名类型
        alipayConfig.setSignType("RSA2");
        //构造client
        return new DefaultAlipayClient(alipayConfig);
    }

}
